package br.unirio.webdisco.entity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.Cookie;

import br.unirio.webdisco.dao.conn.PostgresConnection;

public class Login extends PostgresConnection{
		
		public Usuario logaUsuario(String email, String senha) throws Exception
		{
			Connection connection = criaConexao(false);
			String sql = "SELECT * FROM usuario WHERE email = ? AND senha = ? ";
			PreparedStatement statement =  null;
			ResultSet rs = null;
			Usuario usuario = null;
			
			try {
				statement = connection.prepareStatement(sql);
				statement.setString(1, email);
				statement.setString(2, senha);
				rs = statement.executeQuery();
				
				if (rs.next()) {
					usuario = new Usuario();
					usuario.setIdUsuario(rs.getInt("idusuario"));
					usuario.setIdPerfil(rs.getInt("idperfil"));
					usuario.setNome(rs.getString("nome"));
					usuario.setSobreNome(rs.getString("sobreNome"));
					usuario.setEmail(rs.getString("email"));
					usuario.setSenha(rs.getString("senha"));
				}
				else {
					return null;
				}
			}
			catch (SQLException e) {
				System.out.println(e.getMessage());
			}
			finally {
				try {
					if(statement != null)
						statement.close();
					if(rs != null)
						rs.close();
					this.fechaConexao();
				}
				catch (SQLException e) {
					System.out.println(e.getMessage());
				}
			}
			return usuario;	
		}
};

